package com.aijia.custom.product.ui;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aijia.custom.product.R;
import com.aijia.custom.product.customview.ClearEditText;
import com.aijia.custom.product.customview.CustomDatePicker;
import com.aijia.custom.product.customview.LoadingWaitDialog;
import com.aijia.custom.product.customview.NoDoubleClickListener;
import com.aijia.custom.product.data.SheetData;
import com.aijia.custom.product.data.WSheetData;
import com.aijia.custom.product.listener.IWSheetAddListener;
import com.aijia.custom.product.manager.WSheetAddManager;
import com.aijia.custom.product.utils.AddOrderHelper;
import com.aijia.custom.product.utils.Notification;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by yejianjun on 2019/4/23.
 */

public class AddOrderActivity extends Activity implements IWSheetAddListener {

    private TextView titleTV, sendTV;
    private ImageButton rightBtn, backBtn;
    private WSheetAddManager manager;
    private TextView createDateTV;
    private ClearEditText shopNameET, wwET, proNameET, nameET, wxET, mobileET, addressET, contentET;
    private LinearLayout createDateLayout;
    private String createDate = null;  // 完成时间 格式：yyyy-MM-dd
    private CustomDatePicker customDatePicker;
    private Dialog pDialog;
    private SheetData sheetData;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_order);
        initView();
        initManager();
        initData();
        initDatePicker();
    }

    private void initView() {
        titleTV = findViewById(R.id.title_tv);
        titleTV.setText("新订单");
        sendTV = findViewById(R.id.right_text_tv);
        rightBtn = findViewById(R.id.right_imageButton);
        backBtn = findViewById(R.id.left_imageButton);
        rightBtn.setVisibility(View.GONE);
        backBtn.setVisibility(View.VISIBLE);
        sendTV.setVisibility(View.VISIBLE);
        sendTV.setOnClickListener(noDoubleClickListener);
        backBtn.setOnClickListener(noDoubleClickListener);

        createDateTV = findViewById(R.id.createDate_tv);
        shopNameET = findViewById(R.id.shop_name_et);
        wwET = findViewById(R.id.ww_id_et);
        proNameET = findViewById(R.id.product_name_et);
        nameET = findViewById(R.id.order_name_et);
        wxET = findViewById(R.id.order_wechat_et);
        mobileET = findViewById(R.id.order_mobile_et);
        addressET = findViewById(R.id.order_address_et);
        contentET = findViewById(R.id.order_content_et);
        createDateLayout = findViewById(R.id.createDate_layout);
        createDateLayout.setOnClickListener(noDoubleClickListener);
    }

    private void initManager() {
        manager = new WSheetAddManager(this);
        manager.addWSheetAddListener(this);
    }

    private void initData() {
        sheetData = getIntent().getParcelableExtra("order_item");
        if (null != sheetData) {
            display(sheetData);
        }
    }

    private void reqData() {
        Integer id = null;
        if (null != sheetData)
            id = sheetData.getId();

        manager.wSheetAdd(id, shopNameET.getText().toString(), wwET.getText().toString(), proNameET.getText().toString(), nameET.getText().toString(), wxET.getText().toString(), mobileET.getText().toString(),
                addressET.getText().toString(), contentET.getText().toString(), createDateTV.getText().toString());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != manager)
            manager.removeWSheetAddListener(this);
    }

    NoDoubleClickListener noDoubleClickListener = new NoDoubleClickListener() {
        @Override
        public void onNoDoubleClick(View v) {
            switch (v.getId()) {
                case R.id.right_text_tv: // 发布
                    if (check()) {
                        reqData();
                    }
                    break;
                case R.id.left_imageButton:
                    finish();
                    break;
                case R.id.createDate_layout:
                    // 修改时间选择器
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
                    String now = sdf.format(new Date());
                    if (TextUtils.isEmpty(createDate)) {
                        customDatePicker.show(now);
                    } else {
                        customDatePicker.show(createDate);
                    }
                    break;
                default:
                    break;
            }
        }
    };

    private boolean check() {
        boolean ret = false;
        if (TextUtils.isEmpty(shopNameET.getText().toString().trim())) {
            Notification.toast(this, "店铺名不能为空");
            shopNameET.requestFocus();
            return ret;
        } else if (TextUtils.isEmpty(proNameET.getText().toString().trim())) {
            Notification.toast(this, "产品不能为空");
            proNameET.requestFocus();
            return ret;
        } else if (TextUtils.isEmpty(nameET.getText().toString().trim())) {
            Notification.toast(this, "名称不能为空");
            nameET.requestFocus();
            return ret;
        } else if (TextUtils.isEmpty(createDateTV.getText().toString())) {
            Notification.toast(this, "日期不能为空");
            createDateLayout.requestFocus();
            return ret;
        }
        return true;
    }


    @Override
    public void onWSheetAddFailure(int errorCode, String errorMsg) {
        Notification.toast(this, errorMsg == null ? "" : errorMsg);
    }

    @Override
    public void onWSheetAddSuccess(WSheetData data) {
        // 添加一条数据 通知列表刷新
        AddOrderHelper.getInstance().refreshDate();
        // 修改 则刷新数据
        if (null != sheetData) {
            AddOrderHelper.getInstance().updateInfo(data);
        }
        finish();
    }

    private void showDialog() {
        pDialog = LoadingWaitDialog.createLoadingDialog(this, "");
        pDialog.show();
    }

    private void initDatePicker() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        String now = sdf.format(new Date());
        customDatePicker = new CustomDatePicker(this, new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) { // 回调接口，获得选中的时间
                System.out.println(" time: " + time);
//                showDialog();
                createDate = time;
                createDateTV.setText(time.substring(0, 10));
            }
        }, "1980-01-01", now); // 初始化日期格式请用：yyyy-MM-dd，否则不能正常运行
        customDatePicker.setIsLoop(true); // 允许循环滚动

    }

    private void display(SheetData data) {
        if (null == data)
            return;
        createDateTV.setText(data.getUnderDate() == null ? "" : data.getUnderDate());
        shopNameET.setText(data.getShopName() == null ? "" : data.getShopName());
        wwET.setText(data.getWwId() == null ? "" : data.getWwId());
        proNameET.setText(data.getProductName() == null ? "" : data.getProductName());
        nameET.setText(data.getName() == null ? "" : data.getName());
        wxET.setText(data.getWx() == null ? "" : data.getWx());
        mobileET.setText(data.getMobile() == null ? "" : data.getMobile());
        addressET.setText(data.getAddress() == null ? "" : data.getAddress());
        contentET.setText(data.getContent() == null ? "" : data.getContent());
    }


}
